package com.dahaiwuliang.code.controller.admin;

import com.dahaiwuliang.code.pojo.Comment;
import com.dahaiwuliang.code.server.CommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author bzd
 */
@RestController
@RequestMapping (value = "/admin/comment")
public class CommentAdminController {

    @Autowired
    private CommentService commentService;

    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "分页查询评论信息")
    public Map<String,Object> list(Comment s_comment, @RequestParam(value = "page",required = false)Integer page,@RequestParam(value = "pageSize",required = false)Integer
                                   pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("data",commentService.List(s_comment,page,pageSize,Sort.Direction.DESC,"commentDate").getContent());
        map.put("total",commentService.getTotal(s_comment));
        map.put("errorNo",0);
        return map;
    }

    /**
     * 批量删除评论资源
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions(value = "删除评论资源")
    public Map<String,Object> delete(@RequestParam(value = "commentId")String ids){
        Map<String,Object> resultMap=new HashMap<>();
        String[] idsStr=ids.split(",");
        for (int i=0;i<idsStr.length;i++){
            commentService.delete(Integer.parseInt(idsStr[i]));
        }
        resultMap.put("errorNo",0);
        return resultMap;
    }

    /**
     *
     */
    @RequestMapping(value = "/updateState")
    @RequiresPermissions(value = "修改评论状态")
    public Map<String,Object> updateState(Integer commentId,boolean state){
        Comment comment=commentService.get(commentId);
        if (state){
            comment.setState(1);
        }else {
            comment.setState(2);
        }
        commentService.save(comment);
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        return map;
    }

}
